#!/usr/bin/perl
#create_group.cgi

require "./mdmgroups-lib.pl";
&ReadParse();
&foreign_require("mdmsearch","mdmsearch-lib.pl");

&header($text{'create_title'}, "", "create", 0, 1);
local @devlist=&foreign_call("mdmsearch","list_controlledservs");
print "<form action=save_group.cgi method=post onsubmit='return checkselect(\"$text{'no_groupame'}\");'>\n";
print "<input name=savetype type=hidden value ='new'>";
print "<table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<tr><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<tr><td><b>$text{'input_gpname'}</b></td>\n";
print "<td><input name=gpname value=''></td></tr>\n";
print "</table></td></tr>";
print "<tr><td><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<td><b>$text{'groupmem'}</b></td>";
print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<tr><td>$text{'grouplist'}</td></tr>";
print "<tr><td><select class=list name=memberlist size=12 multiple>";
print "</select></td></tr>";
print "<input name=listinput type=hidden value = ''>";
print "</table></td>";
print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<tr><td><input class=sbutton type=button onclick='addallselect();' value=\'$text{'btaddall'}\'></td></tr>";
print "<tr><td><input class=sbutton type=button onclick='removeallselect();' value=\'$text{'btremoveall'}\'></td></tr>\n";
print "<tr><td><input class=sbutton type=button onclick='addselect();' value=\'$text{'btadd'}\'></td></tr>\n";
print "<tr><td><input class=sbutton type=button onclick='removeselect();' value=\'$text{'btremove'}\'></td></tr>\n";
print "</table></td>";
print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<tr><td>$text{'devlist'}</td></tr>";
print "<tr><td><select class=list name=devicelist size=12 multiple>";
print "<option value=\"[This server]\">[This server]\n";
foreach(@devlist){
	print "<option value=\"[$_->{'id'}]$_->{'ip'}\">[$_->{'id'}]$_->{'ip'}\n";
}
print "</select></td></tr>";
print "</table></td>";
print "</table></tr>\n";
print "<tr><td colspan=2><input class=button1 type=submit name=savebtn value='$text{'submit'}'></td></tr>\n";
print "</table></form>\n";

print <<EOF;
<script>
function find_item_in_grouplist(text1)
{
	var rt = 0;
	var num;
	for(num = 0; num < document.forms[0].memberlist.options.length; num++)
	{
		if(document.forms[0].memberlist.options[num].text == text1)
		{
			rt = 1;
		}
	}
	return rt;
}

function addallselect()
{
	for(i = document.forms[0].memberlist.options.length; i > 0; i--)
	{
		document.forms[0].memberlist.options[i - 1] = null;
	}

	for(i = 0; i < document.forms[0].devicelist.options.length; i++)
	{
		var text1 = document.forms[0].devicelist.options[i].text;
		var option1 = new Option(text1,text1);
		document.forms[0].memberlist.options[i] = option1;
	}

	if(document.forms[0].memberlist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].memberlist.options.length; i++)
	{
		document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
	}
}


function removeallselect()
{
	for(i = document.forms[0].memberlist.options.length; i > 0; i--)
	{
		document.forms[0].memberlist.options[i - 1] = null;
	}

	document.forms[0].listinput.value = "";
}

function addselect()
{
	var j= document.forms[0].memberlist.options.length;

	for(i = document.forms[0].devicelist.options.length; i > 0; i--)
	{
		if(document.forms[0].devicelist.options[i - 1].selected)
		{

			if(find_item_in_grouplist(document.forms[0].devicelist.options[i - 1].text) == 0)
			{
				document.forms[0].memberlist.options[j] = new Option(document.forms[0].devicelist.options[i - 1].text, document.forms[0].devicelist.options[i - 1].text);
				j = document.forms[0].memberlist.options.length;
			}
		}
	}

	if(document.forms[0].memberlist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].memberlist.options.length; i++)
	{
		document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
	}
}

function removeselect()
{
	for(i = document.forms[0].memberlist.options.length; i > 0; i--)
	{
		if(document.forms[0].memberlist.options[i - 1].selected)
		{
			document.forms[0].memberlist.options[i - 1] = null;
		}
	}

	if(document.forms[0].memberlist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].memberlist.options.length; i++)
	{
		document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
	}
	//history.go(0);
}
function checkselect(alertmessg)
{
	if (document.forms[0].gpname.value == ""){
		alert(alertmessg);
		return false;
	}
	
	if(document.forms[0].listinput.value == "")
	{
		if(document.forms[0].memberlist.options.length > 0){
			document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
		}else{
			document.forms[0].listinput.value = "";
		}	

		for(i = 1; i < document.forms[0].memberlist.options.length; i++){
			document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
		}
	}
}
</script>
EOF

&footer("index.cgi",$text{'index_title'});
1;
